package top.buaarobot.webserver.web.rest;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import top.buaarobot.webserver.domain.Messageboard;
import top.buaarobot.webserver.repository.MessageboardRepository;

@RestController
@RequestMapping("/api")
public class MessageboardResource {

    private final MessageboardRepository messageboardRepository;

    public MessageboardResource(MessageboardRepository messageboardRepository) {
        this.messageboardRepository = messageboardRepository;
    }

    @GetMapping(value = "/messageboard")
    public ResponseEntity<Iterable<Messageboard>> getAllMessage() {
        Iterable<Messageboard> messageboards = messageboardRepository.findAll();
        return ResponseEntity.ok(messageboards);
    }

    @PostMapping(value = "/messageboard")
    public ResponseEntity<String> addMessage(@RequestBody JSONObject json){
        String date = json.getString("date");
        String msg = json.getString("msg");
        Messageboard messageboard = new Messageboard();
        messageboard.setDate(date);
        messageboard.setMsg(msg);
        this.messageboardRepository.save(messageboard);
        return ResponseEntity.ok().build();
    }
}
